package com.cz.modules.gc.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cz.modules.cw.service.CwParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.gc.dao.GcToiletDao;
import com.cz.modules.gc.entity.GcToiletEntity;
import com.cz.modules.gc.service.GcToiletService;


@Service("gcToiletService")
public class GcToiletServiceImpl extends ServiceImpl<GcToiletDao, GcToiletEntity> implements GcToiletService {

    @Autowired
    private CwParkingService cwParkingService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GcToiletEntity> page = this.page(
                new Query<GcToiletEntity>().getPage(params),
                new QueryWrapper<GcToiletEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List gcCount(String deptId, String startTime, String endTime) {
        List list=new ArrayList();



        Long start= DateUtil.parse(startTime).getTime();
        Long end=DateUtil.parse(endTime).getTime();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date(start));
        List<Long> timeList=new ArrayList<>();
        for (Long i = start; i <=end ; i=calendar.getTimeInMillis()) {
            timeList.add(calendar.getTimeInMillis());
            calendar.add(Calendar.DAY_OF_MONTH,1);
        }
        List<String> timeStrList=new ArrayList<>();
        for (Long ltime:timeList) {
            timeStrList.add(DateUtil.format(new Date(ltime),"MM-dd"));
        }
        //该时间段内的数据
        List<GcToiletEntity> gclist=this.list(
                new QueryWrapper<GcToiletEntity>()
                .eq("area_id",deptId)
                .ge("rll_date",startTime)
                .le("rll_date",endTime)
        );
        for (String time:timeStrList) {
            int count=0;
            for (GcToiletEntity gc:gclist) {
                String tt=DateUtil.format(gc.getRllDate(),"MM-dd");
                if(tt.equals(time)){
                    count +=Integer.parseInt(gc.getRll());
                }
            }
            list.add(count);
        }
        return list;
    }

    @Override
    public void saverll() {
        String date = DateUtil.format(new Date(),"yyyy-MM-dd");
        GcToiletEntity gc = new GcToiletEntity();
        List<GcToiletEntity> gctelist = this.list(
                new QueryWrapper<GcToiletEntity>()
                        .eq("rll_date",date)
        );
        List<String> rllgcnn= cwParkingService.rllgc(0);
        if(rllgcnn !=null && rllgcnn.size()!=0){
            for (GcToiletEntity g:gctelist) {
                if("male".equals(g.getToiletName())&&"0".equals(g.getAreaId())){
                    gc = g;
                    gc.setRll(rllgcnn.get(0));
                }
            }
            if(gc.getPkid()==null){
                gc.setRll(rllgcnn.get(0));
                gc.setAreaId("0");
                gc.setToiletName("male");
                gc.setRllDate(new Date());
            }
            gc.setUpdateDate(new Date());
            this.saveOrUpdate(gc);
            gc = new GcToiletEntity();
        }
        List<String> rllgcnv= cwParkingService.rllgc(2);
        if(rllgcnv !=null && rllgcnv.size()!=0){
            for (GcToiletEntity g:gctelist) {
                if("female".equals(g.getToiletName())&&"0".equals(g.getAreaId())){
                    gc = g;
                    gc.setRll(rllgcnv.get(0));
                }
            }
            if(gc.getPkid()==null){
                gc.setRll(rllgcnv.get(0));
                gc.setAreaId("0");
                gc.setToiletName("female");
                gc.setRllDate(new Date());
            }
            gc.setUpdateDate(new Date());
            this.saveOrUpdate(gc);
            gc = new GcToiletEntity();
        }
        List<String> rllgcsn= cwParkingService.rllgc(1);
        if(rllgcsn !=null && rllgcsn.size()!=0){
            for (GcToiletEntity g:gctelist) {
                if("male".equals(g.getToiletName())&&"1".equals(g.getAreaId())){
                    gc = g;
                    gc.setRll(rllgcsn.get(0));
                }
            }
            if(gc.getPkid()==null){
                gc.setRll(rllgcsn.get(0));
                gc.setAreaId("1");
                gc.setToiletName("male");
                gc.setRllDate(new Date());
            }
            gc.setUpdateDate(new Date());
            this.saveOrUpdate(gc);
            gc=new GcToiletEntity();
        }
        List<String> rllgcsv= cwParkingService.rllgc(3);
        if(rllgcsv !=null && rllgcsv.size()!=0) {
            for (GcToiletEntity g : gctelist) {
                if ("female".equals(g.getToiletName()) && "1".equals(g.getAreaId())) {
                    gc = g;
                    gc.setRll(rllgcsv.get(0));
                }
            }
            if (gc.getPkid() == null) {
                gc.setRll(rllgcsv.get(0));
                gc.setAreaId("1");
                gc.setToiletName("female");
                gc.setRllDate(new Date());
            }
            gc.setUpdateDate(new Date());
            this.saveOrUpdate(gc);
        }
    }
}
